﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using log4net;
using BidFunLib.Entity;
using BidFunLib.EntityHelper;

namespace BidFunLib.Services
{
    public class BidSaleManager
    {
        private static readonly ILog log = LogManager.GetLogger(typeof(BidSaleManager));

        /// <summary>
        /// Singleton instance
        /// </summary>
        private static BidSaleManager instance;

        /// <summary>
        /// Retreive singleton of this class
        /// </summary>
        public static BidSaleManager Instance
        {
            get
            {
                if (instance == null)
                {
                    instance = new BidSaleManager();
                }
                return BidSaleManager.instance;
            }
            set
            {
                BidSaleManager.instance = value;
            }
        }

        /// <summary>
        /// The dao of this manager
        /// </summary>
        private BidSaleDAO dao = new BidSaleDAO();

        
        /// <summary>
        /// 
        /// </summary>
        private BidSaleManager()
        {
        }

        /// <summary>
        /// 
        /// </summary>
        /// <param name="Id"></param>
        /// <returns></returns>
        public BidSale getBidSaleById(Int32 Id)
        {
            BidSale sale = dao.getBidSaleById(Id);
            if (sale != null)
            {
                sale.BidObject = BidObjectManager.Instance.getObjectById(sale.IdBidObject);
                return sale;
            }
            return null;
        }

        /// <summary>
        /// Rebuild BidSale using ID 
        /// </summary>
        /// <param name="Id"></param>
        /// <returns></returns>
        public BidSale getFullBidSaleById(Int32 Id)
        {
            BidSale sale = dao.getBidSaleById(Id);
            if (sale != null)
            {
                sale.BidObject = BidObjectManager.Instance.getObjectById(sale.IdBidObject);
                sale.BidloggerList = BidLoggerManager.Instance.getLoggersByIdBidSale(sale);
                return sale;
            }
            return null;
        }

        /// <summary>
        /// 
        /// </summary>
        /// <param name="bidSale"></param>
        /// <returns></returns>
        public BidSale saveOrUpdate(BidSale bidSale)
        {
            if (bidSale.EndDateTime != null)
            {
                return dao.saveOrUpdateBidSale(bidSale);
            }
            return null;
        }
    }
}
